YUI().use('io', 'node', function(Y) {
  var Yandex = {
    Y: Y,

    Storage: {
      Pages:  [],
      Links:  [],
      YAHTML: '',
      TempL:  []
    },

    E_DIV: '<div id="yandex_serenity" style="position:fixed;top:0;z-index:100;background-color:#fff;width:80%;height:90%;margin-left:10%;margin-top:5%;overflow:auto;border:2px dotted blue;font-size:0.8em"></div>',

    E_TEMP: '<div id="serenity"></div>',

    doc: Y.get(document.body),

    run: function() {
      this.doc.appendChild(this.Y.Node.create(this.E_DIV));
      this._harvest(this._getPages(), 'Pages');
    },

    _harvest: function(elements, key) {
      elements.forEach(function(element, index) {
        var isLast = (index == elements.length-1) ? true : false;
        Y.io(element, {on: {
          success: function(xid, o) {
            Yandex.Storage[key][Yandex.Storage[key].length] = o.responseText;
            if (isLast) {
              Yandex._parseHarvest(key);
            }
          }
        }});
      });
    },

    _parseHarvest: function(key) {
      if (key == 'Pages') {
        this._parsePages();
      } else {
        this._parseLinks();
      }
    },

    _parsePages: function() {
      document.querySelectorAll('a[href^=link_from.xml?]').forEach(function(element) {
        Yandex.Storage.TempL[Yandex.Storage.TempL.length] = element.getAttribute('href');
      });

      this._getLinksFromXhr(this.Storage.Pages, '#serenity a[href^=link_from.xml?]', 'TempL');

      // garbage collection
      this.Storage.Pages = [];

      this._harvest(this.Storage.TempL, 'Links');
    },

    _parseLinks: function() {
      // garbage collection
      this.Storage.TempL = [];

      // xhr
      this._getLinksFromXhr(this.Storage.Links, '#serenity td.state span', 'YAHTML');
      console.info(this.Storage.YAHTML);

      // document.querySelector('#yandex_serenity').innerHTML = '<ol>' + YAHTML + '</ol>';
    },

    _getLinksFromXhr: function(elements, selector, key) {
      elements.forEach(function(html, index) {
        Yandex.doc.appendChild(Yandex.Y.Node.create(Yandex.E_TEMP));

        var serenity = document.querySelector('#serenity');
        serenity.innerHTML = html;

        document.querySelectorAll(selector).forEach(function(element) {
          if (key == 'YAHTML') {
            Yandex.Storage[key] += '<li>' + base2.DOM.Traversal.getTextContent(element) + '</li>';
          } else {
            Yandex.Storage[key][Yandex.Storage[key].length] = element.getAttribute('href');
          }
        });

        Yandex.doc.removeChild(serenity);
      });
    },

    _getPages: function() {
      var text = base2.DOM.Traversal.getTextContent(document.querySelector('table table:not([class]) td:not([class]) > span')),
        parser = new base2.RegGrp({'Ссылающихся сайтов: ': ''}),
       numbers = parseInt(parser.exec(text), 10),
         count = parseInt((numbers / 25), 10);

      if ((25 * count) != numbers) {
        count++;
      }

      var pagesArray = [];
      for (var iterator = 1; iterator < count; iterator++) {
        pagesArray[pagesArray.length] = location.href + '&page_num=' + iterator;
      }

      return pagesArray;
    }
  };

  Yandex.run();
});